package com.twu.biblioteca;

import java.io.PrintWriter;
import java.util.*;

public class BibliotecaApp {

    private Scanner in;
    private PrintWriter out;
    private User user = null;


    public BibliotecaApp(Scanner scanner, PrintWriter out)
    {
        this.in = scanner;
        this.out = out;
    }

    public void run(){
        out.println("Hello, welcome to Biblioteca");

        out.println("Login Y/N?");
        if(in.hasNext())
        {
            String s = in.nextLine();
            if(s.equalsIgnoreCase("Y"))
                Login();
        }
        boolean exit = false;
        while(!exit)
        {
            PrintMenu();
            if(in.hasNext())
            {
                String s = in.nextLine();
                if(s.startsWith("1"))
                {
                    ViewAllBooks();
                }
                else if(s.startsWith("2"))
                {
                    ReserveBook();
                }
                else if(s.startsWith("3"))
                {
                    MembershipDetails();
                }
                else if (s.startsWith("4"))
                {
                    ExitApplication();
                    exit = true;
                }
                else if (s.startsWith("5"))
                {
                    ViewAllMovies();
                }
                else if (s.startsWith("6"))
                {
                    DisplayMovie();
                }
                else if (s.startsWith("7"))
                {
                    Login();
                }
                else
                {
                    InvalidOption();
                }
            }
        }
    }


    private void Login()
    {
        Data d = Data.GetInstance();
        out.println("Please enter your Username:");
        if(in.hasNext())
        {
            String userName = in.nextLine();

            out.println("Password:");
            if(in.hasNext())
            {
                String password = in.nextLine();
                this.user = d.UserLogon(userName, password);
                if(this.user != null)
                {
                    out.println("Thank you, you have successfully been logged in");
                }
                else
                {
                    out.println("The Username or Password you entered was incorrect.");
                }
            }
        }

    }


    private void DisplayMovie()
    {
        if(this.user != null)
        {
            Data d = Data.GetInstance();
            out.println("Please enter the name of the movie");
            if(in.hasNext())
            {
                String movieTitle = in.nextLine();
                Movie m = d.GetMovie(movieTitle);
                if(m == null)
                {
                    out.println("Sorry we don't have that movie in the library.");
                }
                else
                {
                    out.println("Movie\tYear\tDirector\tRating");
                    out.println(m.toString());
                }
            }
        }
        else
        {
            out.println(ErrorMessages.NOT_LOGGED_IN.toString());
        }

    }
    private void ViewAllMovies() {
        Data d = Data.GetInstance();
        Movie[] allMovies = d.GetAllMovies();
        for(int i = 0; i < allMovies.length; i++)
        {
            out.println(allMovies[i].GetTitle());
        }
    }

    private void PrintMenu() {
        Data d = Data.GetInstance();
        List<String> menuItems = d.GetMenu();
        for(int i = 0; i < menuItems.size(); i++)
        {
            out.println(menuItems.get(i));
        }
    }

    private void InvalidOption() {
        out.println("Select a valid option!!");
    }

    private void ExitApplication() {
        out.println("Application Exiting");
    }

    private void MembershipDetails() {
        if(this.user != null)
        {
            out.println("Name\tEmail\tPhone");
            out.println(String.format("%s\t%s\t%s", user.GetName(), user.GetEmail(), user.GetPhoneNumber()));
        }
        else
        {
            out.println(ErrorMessages.NOT_LOGGED_IN.toString());
        }

    }

    private void ViewAllBooks() {
        Data d = Data.GetInstance();
        out.println("View all Books selected");
        List<Book> books = d.GetAllBooks();
        for(int i = 0; i < books.size(); i++)
        {
            out.println(books.get(i).GetName());
        }
    }

    private void ReserveBook() {
        if(this.user != null)
        {
            Data d = Data.GetInstance();
            out.println("Enter the Name of the book you would like to reserve");
            if(in.hasNext())
            {
                String s = in.nextLine();
                boolean reserve =  d.ReserveBook(s);
                if(reserve)
                {
                    out.println("Thank You! Enjoy the book");
                }
                else
                {
                    out.println("Sorry we don't have that book yet.");
                }
            }
        }
        else
        {
            out.println(ErrorMessages.NOT_LOGGED_IN.toString());
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter(System.getProperty("line.separator"));

        BibliotecaApp app = new BibliotecaApp(scanner, new PrintWriter(System.out, true));
        app.run();
    }
}
